package main.剑指OfferⅠ;

import java.util.Arrays;

public class Day22 {
    public static void main(String[] args) {
        System.out.println("剑指Offer56-Ⅰ.数组中数字出现的次数");
        System.out.println("剑指Offer56-Ⅱ.数组中数字出现的次数Ⅱ");
    }
}

class Offer56_Ⅰ{
    public int[] singleNumbers(int[] nums) {
        int temp=nums[0];
        int[] result=new int[2];
        for (int i=1;i< nums.length;i++) temp=temp^nums[i];
        int index=0;
        for (int i=0;i<32;i++){
            if ((temp>>i&1)==1){
                index=i;
                break;
            }
        }
        for (int i=0;i< nums.length;i++){
            if ((nums[i]>>index&1)==1){
                result[0]=result[0]^nums[i];
            }else {
                result[1]=result[1]^nums[i];
            }
        }
        return result;
    }
}

class Offer56_Ⅱ{
    public int singleNumber(int[] nums) {
        Arrays.sort(nums);
        int result=0,i=0;
        for (i=0;i<nums.length;){
            if (i+2<nums.length&&nums[i]==nums[i+2]) i+=3;
            else break;
        }
        result=nums[i];
        return result;
    }
}